//----------------------------Robustness Check------------------------------

clear all
set more off

*Defining the working folder

global main "insert your directory"
global data "$main/cep_consolidada_90"

*Data is available in Centro de Estudios Públicos website (https://www.cepchile.cl/opinion-publica/encuesta-cep/)

*Load CEP survey database

use "$data"

//////////////////////////////////////////////////////////////////////////
//-----------------Variables: creation and modification------------------
//////////////////////////////////////////////////////////////////////////


*Government approval

gen approval=.
replace approval=1 if eval_gob_1==1
replace approval=0 if eval_gob_1==2

//-----------------------------------------------
//---Economic perceptions variables--------
//-----------------------------------------------

*Egotropic variables 

gen ego_current=.
replace ego_current=2 if percepcion_5==4 | percepcion_5==5
replace ego_current=1 if percepcion_5==3
replace ego_current=0 if percepcion_5==1 | percepcion_5==2

gen ego_prosp=.
replace ego_prosp=2 if percepcion_6==1 | percepcion_6==2
replace ego_prosp=1 if percepcion_6==3
replace ego_prosp=0 if percepcion_6==4 | percepcion_6==5


*Sociotropic variables 

gen socio_current=.
replace socio_current=0 if percepcion_2==1 | percepcion_2==2
replace socio_current=1 if percepcion_2==3
replace socio_current=2 if percepcion_2==4 | percepcion_2==5

gen socio_prosp=.
replace socio_prosp=0 if percepcion_3==3 
replace socio_prosp=1 if percepcion_3==2 
replace socio_prosp=2 if percepcion_3==1 

//--------------------------------
//---Noneconomic variables---------
//--------------------------------

*Age

gen age_group="."
replace age_group="18-24" if (edad>=18 & edad<25) | (edad_cat_1==1)
replace age_group="25-34" if (edad>=25 & edad<35) | (edad_cat_1==2)
replace age_group="35-54" if (edad>=35 & edad<55) | (edad_cat_1==3|edad_cat_1==4)
replace age_group="55+" if (edad>=55 & edad<100) | (edad_cat_1==5 | edad_cat_1==6)
drop if age_group=="."

*Generate indicators for age groups

tab age_group, gen(age_g)

*Woman

gen woman=sexo-1

*Regional dummies

tab region, gen(region_)

*Education level

gen educ_group=.
replace educ_group=1 if esc_nivel_1==0 | esc_tramos==0 // No education
replace educ_group=2 if esc_nivel_1==1 | esc_nivel_1==2  | esc_tramos==2 | esc_tramos==3 | esc_tramos==4 // Primary education
replace educ_group=3 if esc_nivel_1==3 | esc_nivel_1==4 | esc_tramos==5 | esc_tramos==6 // Secondary education
replace educ_group=4 if (esc_nivel_1>=5 & esc_nivel_1<=10) | esc_tramos==7 | esc_tramos==8 // Tertiary education

*Generate indicators for educational groups

tab educ_group, gen(educ_)

*Confidence in political parties

gen conf_pp=.
replace conf_pp=1 if confianza_6_j==1|confianza_6_j==2
replace conf_pp=0 if confianza_6_j==3|confianza_6_j==4

*Survey year indicators

tab encuesta_a, gen(year)

*Political identification (note: In the consolidated database of the CEP survey, the questions that allow creating the "political identification" variable vary depending on the year)

rename encuesta_a year

*Using iden_pol_2 question

gen political_identification = .
replace political_identification = 1 if (iden_pol_2>=1 & iden_pol_2<=5) & year<2010
replace political_identification = 0 if (iden_pol_2>=6 & iden_pol_2<=10) & year<2010
replace political_identification = 1 if (iden_pol_2>=5 & iden_pol_2<=10) & (year>=2010 & year<=2013)
replace political_identification = 0 if (iden_pol_2>=1 & iden_pol_2<=4) & (year>=2010 & year<=2013)
replace political_identification = 1 if (iden_pol_2>=1 & iden_pol_2<=5) & (year>=2014 & year<2018)
replace political_identification = 0 if (iden_pol_2>=6 & iden_pol_2<=10) & (year>=2014 & year<2018)
replace political_identification= 0 if (iden_pol_2>=1 & iden_pol_2<=5) & (year>=2018 & year<2022)
replace political_identification= 1 if (iden_pol_2>=6 & iden_pol_2<=10) & (year>=2018 & year<2022)
replace political_identification=1 if (iden_pol_2>=1 & iden_pol_2<=5) & year==2022
replace political_identification=0 if (iden_pol_2>=6 & iden_pol_2<=10) & year==2022

*Using iden_pol_1 question

replace political_identification=1 if (iden_pol_1==4|iden_pol_1==5 |iden_pol_1==3) & year<2010 & political_identification==.
replace political_identification=0 if (iden_pol_1==1 | iden_pol_1==2) & year<2010 & political_identification==.

replace political_identification=1 if (iden_pol_1==1 | iden_pol_1==2|iden_pol_1==3) & (year>=2010 & year<=2013) & political_identification==.
replace political_identification=0 if (iden_pol_1==4|iden_pol_1==5) & (year>=2010 & year<=2013) & political_identification==.

replace political_identification=1 if (iden_pol_1==4|iden_pol_1==5 |iden_pol_1==3) & (year>=2014 & year<2018) & political_identification==.
replace political_identification=0 if (iden_pol_1==1 | iden_pol_1==2) & (year>=2014 & year<2018) & political_identification==.

replace political_identification=1 if (iden_pol_1==4|iden_pol_1==5|iden_pol_1==3) & (year>=2018 & year<2022) & political_identification==.
replace political_identification=0 if (iden_pol_1==4|iden_pol_1==5) & (year>=2018 & year<2022) & political_identification==.

replace political_identification=1 if (iden_pol_1==4 | iden_pol_1==5 | iden_pol_1==3) & year==2022 & political_identification==.
replace political_identification=0 if (iden_pol_1==1 | iden_pol_1==2) & year==2022 & political_identification==.

*Using iden_pol_1_a question


replace political_identification=1 if (iden_pol_1_a==1 | iden_pol_1_a==2 | iden_pol_1_a==3) & (year>=2010 & year<=2013) & political_identification==.
replace political_identification=0 if (iden_pol_1_a==4|iden_pol_1_a==5) & (year>=2010 & year<=2013) & political_identification==.

replace political_identification=1 if (iden_pol_1_a==4|iden_pol_1_a==5|iden_pol_1_a==3) & (year>=2014 & year<2018) & political_identification==.
replace political_identification=0 if (iden_pol_1_a==1 | iden_pol_1_a==2) & (year>=2014 & year<2018) & political_identification==.

replace political_identification=1 if (iden_pol_1_a==1 | iden_pol_1_a==2|iden_pol_1_a==3) & (year>=2018 & year<2022) & political_identification==.
replace political_identification=0 if (iden_pol_1_a==4|iden_pol_1_a==5) & (year>=2018 & year<2022) & political_identification==.

replace political_identification=1 if (iden_pol_1_a==4|iden_pol_1_a==5|iden_pol_1_a==3) & year==2022 & political_identification==.
replace political_identification=0 if (iden_pol_1_a==1 | iden_pol_1_a==2) & year==2022 & political_identification==.


///////////////////////////////////////////////////////
//-----------------Logit regression--------------------
///////////////////////////////////////////////////////

global year "year1 year2 year3 year4 year5 year6 year7 year8 year9 year10 year11 year12 year13 year14 year15 year16 year17 year18 year19 year20 year21 year22 year23 year24 year25 year26 year27 year28 year29 year30 year31 year32"
global ne "woman age_g1 age_g2 age_g3 age_g4 educ_1 educ_2 educ_3 educ_4 political_identification"

//-----------------
// Table 5 results
//-----------------

*Column 1

logit approval ego_current socio_current $ne $year region_* [pw=pond], r
margins, dydx(ego_current socio_current $ne) post
outreg2 using "tabla5", lab word stats(coef se) replace keep(ego_current socio_current $ne) addtext(Region dummies, Yes, Year dummies, Yes, Robust SE, Yes) dec(2)

*Column 2
logit approval ego_prosp socio_prosp $ne $year region_* [pw=pond], r
margins, dydx(ego_prosp socio_prosp $ne) post
outreg2 using "tabla5", lab word stats(coef se) append keep(ego_prosp socio_prosp $ne) addtext(Region dummies, Yes, Year dummies, Yes, Robust SE, Yes) dec(2)

*Column 3
logit approval ego_current socio_current ego_prosp socio_prosp $ne $year region_* [pw=pond], r
margins, dydx(ego_current socio_current ego_prosp socio_prosp $ne) post
outreg2 using "tabla5", lab word stats(coef se) append keep(ego_current socio_current ego_prosp socio_prosp $ne) sortvar(ego_current socio_current ego_prosp socio_prosp $ne) addtext(Region dummies, Yes, Year dummies, Yes, Robust SE, Yes) dec(2)


//-----------------
// Figure 5 results
//-----------------

forvalues j=16/32{
	gen sc_year`j'=year`j'*socio_current
	gen sp_year`j'=year`j'*socio_prosp
	gen ec_year`j'=year`j'*ego_current
	gen ep_year`j'=year`j'*ego_prosp
}

global sc_interactions "sc_year18 sc_year19 sc_year20 sc_year21 sc_year22 sc_year23 sc_year24 sc_year25 sc_year27 sc_year28 sc_year29 sc_year30 sc_year31 sc_year32"
global sp_interactions "sp_year18 sp_year19 sp_year20 sp_year21 sp_year22 sp_year23 sp_year24 sp_year25 sp_year27 sp_year28 sp_year29 sp_year30 sp_year31 sp_year32"
global ec_interactions "ec_year18 ec_year19 ec_year20 ec_year21 ec_year22 ec_year23 ec_year24 ec_year25 ec_year27 ec_year28 ec_year29 ec_year30 ec_year31 ec_year32"
global ep_interactions "ep_year18 ep_year19 ep_year20 ep_year21 ep_year22 ep_year23 ep_year24 ep_year25 ep_year27 ep_year28 ep_year29 ep_year30 ep_year31 ep_year32"
global all_interactions "$sc_interactions $sp_interactions $ec_interactions $ep_interactions"
global year_dummies "year18 year19 year20 year21 year22 year23 year24 year25 year27 year28 year29 year30 year31 year32"


set scheme white_piyg

*Panel A

logit approval ego_current socio_current ego_prosp socio_prosp $ne $year_dummies $all_interactions  region_* [pw=pond], r
margins, dydx(ec_*) post
coefplot, vertical keep(ec_*) yline(0) ciopts(recast(rcap)) coeflabel (ec_year18= "2007" ec_year19 = "2008" ec_year20 = "2009" ec_year21 = "2010" ec_year22 = "2011" ec_year23 = "2012" ec_year24 = "2013" ec_year25 = "2014" ec_year26 = "2015" ec_year27 = "2016" ec_year28 = "2017" ec_year29 = "2018" ec_year30 = "2019" ec_year31 = "2020" ec_year32 = "2022")

*Panel B
logit approval ego_current socio_current ego_prosp socio_prosp $ne $year_dummies $all_interactions  region_* [pw=pond], r
margins, dydx(ep_*) post
coefplot, vertical keep(ep_*) yline(0) ciopts(recast(rcap)) coeflabel (ep_year18= "2007" ep_year19 = "2008" ep_year20 = "2009" ep_year21 = "2010" ep_year22 = "2011" ep_year23 = "2012" ep_year24 = "2013" ep_year25 = "2014" ep_year26 = "2015" ep_year27 = "2016" ep_year28 = "2017" ep_year29 = "2018" ep_year30 = "2019" ep_year31 = "2020" ep_year32 = "2022")

*Panel C
logit approval ego_current socio_current ego_prosp socio_prosp $ne $year_dummies $all_interactions  region_* [pw=pond], r
margins, dydx(sc_*) post
coefplot, vertical keep(sc_*) yline(0) ciopts(recast(rcap)) coeflabel (sc_year18= "2007" sc_year19 = "2008" sc_year20 = "2009" sc_year21 = "2010" sc_year22 = "2011" sc_year23 = "2012" sc_year24 = "2013" sc_year25 = "2014" sc_year26 = "2015" sc_year27 = "2016" sc_year28 = "2017" sc_year29 = "2018" sc_year30 = "2019" sc_year31 = "2020" sc_year32 = "2022")

*Panel D
logit approval ego_current socio_current ego_prosp socio_prosp $ne $year_dummies $all_interactions  region_* [pw=pond], r
margins, dydx(sp_*) post
coefplot, vertical keep(sp_*) yline(0) ciopts(recast(rcap)) coeflabel (sp_year18= "2007" sp_year19 = "2008" sp_year20 = "2009" sp_year21 = "2010" sp_year22 = "2011" sp_year23 = "2012" sp_year24 = "2013" sp_year25 = "2014" sp_year26 = "2015" sp_year27 = "2016" sp_year28 = "2017" sp_year29 = "2018" sp_year30 = "2019" sp_year31 = "2020" sp_year32 = "2022")


//------------------
// Table A.2 results
//------------------

global ne "woman age_g1 age_g2 age_g3 age_g4 educ_1 educ_2 educ_3 educ_4 political_identification conf_pp"


*Column 1

logit approval ego_current socio_current $ne $year region_* [pw=pond], r
margins, dydx(ego_current socio_current $ne) post
outreg2 using "tablaA2", lab word stats(coef se) replace keep(ego_current socio_current $ne) addtext(Region dummies, Yes, Year dummies, Yes, Robust SE, Yes) dec(2)

*Column 2
logit approval ego_prosp socio_prosp $ne $year region_* [pw=pond], r
margins, dydx(ego_prosp socio_prosp $ne) post
outreg2 using "tablaA2", lab word stats(coef se) append keep(ego_prosp socio_prosp $ne) addtext(Region dummies, Yes, Year dummies, Yes, Robust SE, Yes) dec(2)

*Column 3
logit approval ego_current socio_current ego_prosp socio_prosp $ne $year region_* [pw=pond], r
margins, dydx(ego_current socio_current ego_prosp socio_prosp $ne) post
outreg2 using "tablaA2", lab word stats(coef se) append keep(ego_current socio_current ego_prosp socio_prosp $ne) sortvar(ego_current socio_current ego_prosp socio_prosp $ne) addtext(Region dummies, Yes, Year dummies, Yes, Robust SE, Yes) dec(2)



